package com.qianjiangcloud.store.adapter;

/**
 * 横向滑动商品适配器
 */
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;

import org.json.JSONObject;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;

public class GalleryAdapter extends
        RecyclerView.Adapter<GalleryAdapter.ViewHolder>
{

    private LayoutInflater mInflater;
    private ArrayList<JSONObject> mSlidingGoodsArray;

    public GalleryAdapter(Context context, ArrayList<JSONObject>slidingGoodsArray)
    {
        mInflater = LayoutInflater.from(context);
        mSlidingGoodsArray = slidingGoodsArray;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View view)
        {
            super(view);


            sliding_good_id = (LinearLayout) view
                    .findViewById(R.id.sliding_good_id);
            mImg = (ImageView) view
                    .findViewById(R.id.goods_gallery_item_image);
            goods_gallery_item_text = (TextView) view
                    .findViewById(R.id.goods_gallery_item_text);
            sale_price = (TextView) view
                    .findViewById(R.id.sale_price);
            cost_price = (TextView) view
                    .findViewById(R.id.cost_price);
        }

        ImageView mImg;
        TextView goods_gallery_item_text;
        TextView sale_price;
        TextView cost_price;
        LinearLayout sliding_good_id;

    }

    @Override
    public int getItemCount()
    {
        return mSlidingGoodsArray.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.sliding_goods_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);


        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        JSONObject slidingGoodObject = mSlidingGoodsArray.get(i);
        Uri imageUri = Uri.parse(slidingGoodObject.optString("good_img"));
        viewHolder.mImg.setTag(R.id.tag_object, slidingGoodObject);
        viewHolder.mImg.setTag(imageUri);
        MyImageLoader.displayDefaultImage(slidingGoodObject.optString("good_img"), viewHolder.mImg);
        viewHolder.goods_gallery_item_text.setText(slidingGoodObject.optString("good_title"));
        viewHolder.sale_price.setText("￥"+ slidingGoodObject.optString("sale_price"));
        viewHolder.cost_price.setText("￥"+ slidingGoodObject.optString("cost_price"));


        viewHolder.cost_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰

        viewHolder.sliding_good_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject obj=mSlidingGoodsArray.get(i);
                if (obj==null)
                {
                    return;
                }
                String goodsIID = obj.optString("goods_id");
                Intent intent = AgentActivity.intentForFragment(
                        viewHolder.itemView.getContext(), AgentActivity.FRAGMENT_GOODS_DETAIL)
                        .putExtra(Run.EXTRA_CLASS_ID, goodsIID);
                viewHolder.itemView.getContext().startActivity(intent);

            }
        });
    }

}